<template>
    <div class="app">
        <div>
            <div class="button_saveandfile">
                <div class="radio_search_area" @keyup.enter="keyDown($event)">
                    <span class="demonstration" style="width: 70px;margin-left: -3%;">电台日期:</span>
                    <el-date-picker v-model="searchStartDate" type="date" placeholder="电台日期"></el-date-picker>
                    <!-- 
                        <span class="demonstration">~</span>
                        <el-date-picker v-model="searchEndDate" type="date" placeholder="结束日期"></el-date-picker> -->
                    <el-input v-model="searchRadioName" class="radio_search_input" placeholder="请输入查询电台名"
                        style="margin-left: 2%;" />
                    <el-button type="warning" plain @click="searchRadioPlayProgramDaily" class="radio_search_submit"
                        style="margin-left: 4%;margin-top: -0.3%;">查询</el-button>
                    <el-button type="primary" @click="radioAndProgramDailyInfoExport" class="primary" plain>导出当日电台与节目系数主品数据</el-button>
                </div>
            </div>
            <el-divider></el-divider>
            <el-dialog v-model="dialogFormVisible" title="更新每日电台节目系数">
                <div style="color: red;margin-top:-20px">*注:编辑时电台和节目名称不可以修改</div>
                <el-form>
                    <el-form-item label="电台名称" :label-width="formLabelWidth">
                        <el-input autocomplete="off" v-model="dailyRadioAndProgramAddVO.radioName" placeholder="电台名称"
                            v-bind:readonly="dailyRadioAndProgramAddVO.id != null" />
                    </el-form-item>
                </el-form>
                <el-form>
                    <el-form-item label="节目名称" :label-width="formLabelWidth">
                        <el-input autocomplete="off" v-model="dailyRadioAndProgramAddVO.programName" placeholder="节目名称"
                          v-bind:readonly="dailyRadioAndProgramAddVO.id != null"  />
                    </el-form-item>
                </el-form>
                <el-form>
                    <el-form-item label="节目系数：" :label-width="formLabelWidth">
                        <el-input autocomplete="off" v-model="dailyRadioAndProgramAddVO.xCoefficient" placeholder="节目系数"  style="width:200px"/>
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="cancellation">取消</el-button>
                        <el-button type="primary" @click="saveDailyRadioAndProgram">
                            保存
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
        <!-- -------------------------------------------------------tab-------------------------------------------------- -->
        <div class="tabdiv">
            <el-table :data="radioPlayProgramDailyVOList" style="width:100%;height:615px">
                <el-table-column type="index" width="50" fixed="left">
                </el-table-column>
                <el-table-column label="电台名称" width="250" fixed="left">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.radioProgramVO.radioVO.radioName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="节目名称" width="180" show-overflow-tooltip fixed="left">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.radioProgramVO.programName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="所属电台" width="180" class="xishu">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.radioProgramVO.radioVO.belongRadioName }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="当天节目系数" width="180" class="xishu">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.xCoefficient}}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" width="180" class="xishu">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.createTime }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="更新时间" width="180" class="xishu">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.lastUpdateTime }}</span>
                        </div>
                    </template>
                </el-table-column>
               
                <el-table-column label="修改信息">
                    <template #default="scope">
                        <el-button type="primary" size="small"
                            @click="handleEdit(scope.$index, scope.row.id)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页器 -->
        <div class="block-box">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                :page-sizes="[1, 5, 10, 20]" :page-size="pageSize" :total="total"
                layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
        </div>
    </div>
</template>

<script>

import axios from '../api/index'
import { mapState } from 'vuex'
import { ElLoading } from 'element-plus';
export default {
    // computed:{
    //    ...mapState(['userInfo'])
    // },
    data() {
        return {
            searchRadioName: '',
            searchStartDate: '',
            dialogTableVisible: false,
            dialogFormVisible: false,
            currentPage: 1, // 当前页码
            pageSize: 20, // 每页的数据条数
            total: 0, // 总条数
            radioPlayProgramDailyVOList: [],
            dailyRadioAndProgramAddVO: {
                radioName: '',//电台名称
                programName: '', //节目名称
                xCoefficient: '', //系数
                id: '' //每日电台与节目系数表主键
            },        

        }
    },
    methods: {
        //编辑查询方法------
        handleEdit(index, row) {
            this.dialogFormVisible = true
            // 调ajax后台详情接口(id)
            let base = `/radio/searchDailyRadioAndProgramByDailyId/${row}`
            axios.getRequest(base)
                .then((res) => {
                    if (res.data.code === 200) {
                        this.dailyRadioAndProgramAddVO.radioName = res.data.data.radioProgramVO.radioVO.radioName
                        this.dailyRadioAndProgramAddVO.programName = res.data.data.radioProgramVO.programName
                        this.dailyRadioAndProgramAddVO.xCoefficient = res.data.data.xCoefficient
                        this.dailyRadioAndProgramAddVO.id = row
                        //this.searchRadio()
                    } else {
                        this.$message({
                            type: 'error',
                            message: '编辑每日电台节目系数数据失败！'+res.data.message
                        })
                    }
                })

        },
        //监听 pagesize 改变的事件
        handleSizeChange(newSize) {
            console.log(newSize + '+++++++++++++++++');
            this.pageSize = newSize;//重新指定每页数据量
            this.searchRadioPlayProgramDaily();//带着新的分页请求获取数据
        },
        //监听 页码值 改变的事件
        handleCurrentChange(newPage) {
            console.log(newPage + '-----------------');
            this.currentPage = newPage;//重新指定当前页
            this.searchRadioPlayProgramDaily();//带着新的分页请求获取数据
        },
        saveDailyRadioAndProgram() {
            axios.postRequest('/radio/updateRadioAndProgramDaily', this.dailyRadioAndProgramAddVO)
                .then(res => {
                    if (res.data.code === 200) {
                        this.searchRadioPlayProgramDaily()
                        this.$message({
                            type: 'success',
                            message: '操作成功！'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '操作失败！'+res.data.message
                        })
                    }
                })
            this.dialogFormVisible = false
            this.dailyRadioAndProgramAddVO.radioName = ''
            this.dailyRadioAndProgramAddVO.programName = ''
            this.dailyRadioAndProgramAddVO.xCoefficient = ''
            this.dailyRadioAndProgramAddVO.id = ''
        },
        keyDown(e){
            if (e.key == 'Enter') {
                console.log('回车事件被执行')
                this.searchRadioPlayProgramDaily();
            }
        },
        searchRadioPlayProgramDaily() {
            var date = new Date(this.searchStartDate);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var min = date.getMinutes();
            min = min < 10 ? ('0' + min) : min;
            var s = date.getSeconds();
            s = s < 10 ? ('0' + s) : s;
            this.searchStartDate = y + '-' + m + '-' + d
            console.log(this.searchStartDate)
            var pageInfo = { searchDate: this.searchStartDate,pageNum: this.currentPage, pageSize: this.pageSize,radioName: this.searchRadioName }
            axios.postRequest('/radio/searchRadioPlayProgramPage',pageInfo)
            .then(res => {
                if (res.data.code === 200) {
                    this.radioPlayProgramDailyVOList = res.data.data.radioPlayProgramDailyVOList
                    //注意：分页这里必须要将total转换成数字！！！
                    this.total = parseInt(res.data.data.total)  
                } else {
                    this.$message({
                        type: 'error',
                        message: '数据出错！'+res.data.message
                    })
                }
            })
        },
        cancellation() {
            this.dialogFormVisible = false
            this.dailyRadioAndProgramAddVO.radioName = ''
            this.dailyRadioAndProgramAddVO.programName = ''
            this.dailyRadioAndProgramAddVO.xCoefficient = ''
            this.dailyRadioAndProgramAddVO.id = ''
        },
        //下载文件
        downloadFile(res, fileName) {
            let blob = new Blob([res.data]); //res为从后台返回的数据
            //let blob = new Blob([res]); 
            if (!fileName) {
                fileName = res.headers['content-disposition'].split('filename=').pop();
            }
            if ('msSaveOrOpenBlob' in navigator) {
                window.navigator.msSaveOrOpenBlob(blob, fileName);
            } else {
                const elink = document.createElement('a');
                elink.download = fileName;
                elink.style.display = 'none';
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href);
                document.body.removeChild(elink);
            }
        },
        radioAndProgramDailyInfoExport() {
            axios.getRequest('/radio/getRadioDailyExport?searchStartDate=' + this.searchStartDate + '&radioName=' + this.searchRadioName, null, 'blob')
                .then(res => {
                    this.$message({
                        type: 'warning',
                        message: '正在导出当日电台节目系数主品数据，请耐心等待'
                    })
                    this.downloadFile(res, '当日电台节目系数主品表.xlsx')
                })
        },
        open() {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            var date = new Date(this.searchStartDate);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var min = date.getMinutes();
            min = min < 10 ? ('0' + min) : min;
            var s = date.getSeconds();
            s = s < 10 ? ('0' + s) : s;
            let searchDate = y + '-' + m + '-' + d
            console.log(searchDate )           
            axios.getRequest(`/radio/delete_radioAndHistoryAndStop/${searchStartDate}/${searchEndDate}`)
            .then( res => {
                        this.searchRadioPlayProductDailyRadio();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
            })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }

    },
    mounted() {
        let isDate = new Date()
        // 获取前一天的日期
        isDate.setDate(isDate.getDate() - 1);
        let sTime = `${isDate.getFullYear()}-${isDate.getMonth() + 1}-${isDate.getDate()}`
        // sTime = `${sTime} 00:00:00`
        // eTime = `${eTime} 23:59:59`
        this.searchStartDate = sTime
        this.searchRadioPlayProgramDaily();

        window.addEventListener("keydown", this.keyDown);
    },
    destroyed() {        
         // 销毁事件
      window.addEventListener("keydown", this.keyDown);
    }
}
</script>
    
<style scope>
.radio_search_area {
    margin-top: 20px;
    margin-left: 5px;
    font-size: 14px;
    display: flex;
    padding-left: 25%;
    width: 730px;
    height: 50px;
    line-height: 28px;
}

.radio_search_area .el-input {
    width: 170px;
    height: 30px;
}

.radio_search_area .radio_search_submit {
    margin-left: 10px;
}

.file {
    display: flex;
}

.filea {
    display: flex;
}
.filed{
    display: flex;
    margin-left: 320px;
    margin-top: -73px;
}

.fontb {
    width: 160px;
    font-size: 13px;
    margin-top: 20px;
    margin-left: 5px;
    height: 20px;
}

.demo-ruleForm {
    width: 0px;
    margin-left: -90px;
    margin-top: 6px;
}
.delete_button{
    margin-top: -0.3%;
}
.demo-time-range {
    margin-bottom: 10px;
}

.el-select {
    margin-top: 10px;
    width: 280px;
    margin-left: 12px;
    margin-right: 12px;
}

.jianju {
    height: 50px;
    align-items: center;
    text-align: center;
}


.cell {
    width: 250px;
    text-align: left;
}

body,
hr,
dl,
dt,
dd,
ul,
ol,
li,
h1,
h2,
h3,
h4,
h5,
h6,
pre,
form,
fieldset,
input,
textarea,
blockquote,
p {
    padding: 0;
    margin: 0;
}

.el-dialog__headerbtn {
    display: none;
}

.block-box {
    width: 100%;
    height: 50px;
    margin-top: 14px;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>